var safeBottom = uni.getSystemInfoSync().safeAreaInsets.bottom	
import { GroupCenterApi } from '@/api/serves.js'
import { GoodsCenterApi } from '@/api/serves.js'
export default {
	data() {
		return {
			safeBottom: safeBottom,
			itemId: '',
			details: {},
			evaluation: {             // 评论 只显示一条
				total: '', 
				info: null,
			},
			groupTotal: '',
			groupList: [] // 拼团列表
		}
	},
	computed: {
		titleInfo() {
			return {
				goods_name: this.details.goods_name,
				goods_sales: this.details.virtual_sale_num,
				stock_num: this.details.stock_num,
				address: this.details.merch_info && this.details.merch_info.address_city,
				merch_id: this.details.merch_id
			}
		},
		bottomInfo() {
			return {
				goods_price: this.details.goods_price,
				groupPrice: this.details.price,
				merch_id: this.details.goods && this.details.goods.merch_id,
			}
		},
	},
	onLoad(options) {
		this.itemId = options.id
	},
	onShow() {
		if(this.itemId) this._initData()
	},
	onNavigationBarButtonTap(e) {
		this.share()
	},
	methods: {
		_initData() {
			this._getDetailGroup()
			this._goodsEvaluate()  // 获取商品评价
			this._progress_list()  // 对应商品的团
		},
		_getDetailGroup() {
			uni.showLoading()
			GroupCenterApi.getDetailGroup({id: this.itemId}).then(res => {
				uni.hideLoading()
				console.log('拼团商品详情', res);
				if (res.code == 1) {
					this.details = res.data
				}else {
					uni.showToast({	title: res.msg, icon:'fail'})
				}
			})
		},
		// 获取商品评价
		_goodsEvaluate() {
			GoodsCenterApi.goods_comments({ goods_id: this.itemId }).then(res => {
				console.log('获取商品评价', res);
				if (res.code == 1) {
					this.evaluation.total = res.count || 0
					this.evaluation.info = res.data[0]
				}
			})
		},
		// 对应商品的团
		_progress_list() {
			GroupCenterApi.progress_list({ groups_goods_id: this.itemId, page:1 }).then(res => {
				console.log('对应商品的团', res);
				if (res.code == 1) {
					this.groupTotal = res.count
					this.groupList = res.data
				}else {
					uni.showToast({	title: res.msg, icon:'fail'})
				}
			})
		},
		share() {
			let params = {
				title: this.details.goods_name,
				src:  this.details.imgs_url[0],
				id: this.itemId
			}
			uni.navigateTo({
				url:'/pages/group/grouopDetailCanvas/grouopDetailCanvas?info=' + JSON.stringify(params)
			})
		},
		// 发起拼团
		buyGroup() {
			uni.navigateTo({
				url: '/pages/group/groupOrderAdd/groupOrderAdd?groups_goods_id=' + this.itemId
			})
		},
		buySelf() {
			uni.navigateTo({
			  url:'/pages/goodsDetail/commonGoodsDetail/commonGoodsDetail?goods_id='+ this.details.goods_id
			})
		},
	}
}